/*
 * Copyright 2022 NXP
 * SPDX-License-Identifier: MIT
 */

#include "events_init.h"
#include <stdio.h>
#include "lvgl/lvgl.h"

#if !LV_USE_GUIDER_SIMULATOR
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
extern TaskHandle_t mpuTaskHandle;
#endif

#if !LV_USE_GUIDER_SIMULATOR
#include "driver/gpio.h"
#endif


void events_init(lv_ui *ui)
{
}

static void screen_btn_b1event_handler(lv_obj_t * obj, lv_event_t event)
{
	switch (event)
	{
	case LV_EVENT_CLICKED:
	{
		if (!lv_debug_check_obj_valid(guider_ui.BreathingLight))
			setup_scr_BreathingLight(&guider_ui);
		lv_scr_load_anim(guider_ui.BreathingLight, LV_SCR_LOAD_ANIM_OVER_LEFT, 200, 0, true);
		#if !LV_USE_GUIDER_SIMULATOR
		if(gpio_get_level(2)){
			lv_switch_on(guider_ui.BreathingLight_BL_sw_1, LV_ANIM_ON);
			lv_led_on(guider_ui.BreathingLight_led_1);
		}
		else{
			lv_led_off(guider_ui.BreathingLight_led_1);
		}
		#endif
	}
		break;
	default:
		break;
	}
}

static void screen_btn_b2event_handler(lv_obj_t * obj, lv_event_t event)
{
	switch (event)
	{
	case LV_EVENT_CLICKED:
	{
		if (!lv_debug_check_obj_valid(guider_ui.Voltmeter))
			setup_scr_Voltmeter(&guider_ui);
		lv_scr_load_anim(guider_ui.Voltmeter, LV_SCR_LOAD_ANIM_OVER_LEFT, 200, 0, true);
	}
		break;
	default:
		break;
	}
}

static void screen_btn_b3event_handler(lv_obj_t * obj, lv_event_t event)
{
	switch (event)
	{
	case LV_EVENT_CLICKED:
	{
		if (!lv_debug_check_obj_valid(guider_ui.Gyro))
			setup_scr_Gyro(&guider_ui);
		lv_scr_load_anim(guider_ui.Gyro, LV_SCR_LOAD_ANIM_OVER_LEFT, 200, 0, true);
		#if !LV_USE_GUIDER_SIMULATOR
		vTaskResume(mpuTaskHandle);
		#endif
	}
		break;
	default:
		break;
	}
}

static void screen_btn_b4event_handler(lv_obj_t * obj, lv_event_t event)
{
	switch (event)
	{
	case LV_EVENT_CLICKED:
	{
		if (!lv_debug_check_obj_valid(guider_ui.thermometer))
			setup_scr_thermometer(&guider_ui);
		lv_scr_load_anim(guider_ui.thermometer, LV_SCR_LOAD_ANIM_OVER_LEFT, 200, 0, true);
	}
		break;
	default:
		break;
	}
}

static void screen_btn_a2event_handler(lv_obj_t * obj, lv_event_t event)
{
	switch (event)
	{
	case LV_EVENT_CLICKED:
	{
		if (!lv_debug_check_obj_valid(guider_ui.Calendar))
			setup_scr_Calendar(&guider_ui);
		lv_scr_load_anim(guider_ui.Calendar, LV_SCR_LOAD_ANIM_OVER_LEFT, 200, 0, true);
	}
		break;
	default:
		break;
	}
}

static void screen_btn_a1event_handler(lv_obj_t * obj, lv_event_t event)
{
	switch (event)
	{
	case LV_EVENT_CLICKED:
	{
		if (!lv_debug_check_obj_valid(guider_ui.MyDevice))
			setup_scr_MyDevice(&guider_ui);
		lv_scr_load_anim(guider_ui.MyDevice, LV_SCR_LOAD_ANIM_OVER_LEFT, 200, 0, true);
	}
		break;
	default:
		break;
	}
}

void events_init_screen(lv_ui *ui)
{
	lv_obj_set_event_cb(ui->screen_btn_b1, screen_btn_b1event_handler);
	lv_obj_set_event_cb(ui->screen_btn_b2, screen_btn_b2event_handler);
	lv_obj_set_event_cb(ui->screen_btn_b3, screen_btn_b3event_handler);
	lv_obj_set_event_cb(ui->screen_btn_b4, screen_btn_b4event_handler);
	lv_obj_set_event_cb(ui->screen_btn_a2, screen_btn_a2event_handler);
	lv_obj_set_event_cb(ui->screen_btn_a1, screen_btn_a1event_handler);
}

static void MyDevice_btn_1event_handler(lv_obj_t * obj, lv_event_t event)
{
	switch (event)
	{
	case LV_EVENT_CLICKED:
	{
		
	}
		break;
	default:
		break;
	}
}

static void MyDevice_backevent_handler(lv_obj_t * obj, lv_event_t event)
{
	switch (event)
	{
	case LV_EVENT_CLICKED:
	{
		if (!lv_debug_check_obj_valid(guider_ui.screen))
			setup_scr_screen(&guider_ui);
		lv_scr_load_anim(guider_ui.screen, LV_SCR_LOAD_ANIM_OVER_RIGHT, 200, 0, true);
	}
		break;
	default:
		break;
	}
}

void events_init_MyDevice(lv_ui *ui)
{
	lv_obj_set_event_cb(ui->MyDevice_btn_1, MyDevice_btn_1event_handler);
	lv_obj_set_event_cb(ui->MyDevice_back, MyDevice_backevent_handler);
}

static void Calendar_btn_1event_handler(lv_obj_t * obj, lv_event_t event)
{
	switch (event)
	{
	case LV_EVENT_CLICKED:
	{
		if (!lv_debug_check_obj_valid(guider_ui.screen))
			setup_scr_screen(&guider_ui);
		lv_scr_load_anim(guider_ui.screen, LV_SCR_LOAD_ANIM_OVER_RIGHT, 200, 0, true);
	}
		break;
	default:
		break;
	}
}

void events_init_Calendar(lv_ui *ui)
{
	lv_obj_set_event_cb(ui->Calendar_btn_1, Calendar_btn_1event_handler);
}

static void BreathingLight_BL_sw_1event_handler(lv_obj_t * obj, lv_event_t event)
{
	switch (event)
	{
	case LV_EVENT_VALUE_CHANGED:
	{
		if(lv_switch_get_state(obj)){
		  #if !LV_USE_GUIDER_SIMULATOR
				gpio_set_level(2, 1);
		  #endif
			lv_led_on(guider_ui.BreathingLight_led_1);
		}
		else{
		  #if !LV_USE_GUIDER_SIMULATOR
				gpio_set_level(2, 0);
		  #endif
			lv_led_off(guider_ui.BreathingLight_led_1);
		}
	}
		break;
	default:
		break;
	}
}

static void BreathingLight_btn_1event_handler(lv_obj_t * obj, lv_event_t event)
{
	switch (event)
	{
	case LV_EVENT_CLICKED:
	{
		if (!lv_debug_check_obj_valid(guider_ui.screen))
			setup_scr_screen(&guider_ui);
		lv_scr_load_anim(guider_ui.screen, LV_SCR_LOAD_ANIM_OVER_RIGHT, 200, 0, true);
	}
		break;
	default:
		break;
	}
}

void events_init_BreathingLight(lv_ui *ui)
{
	lv_obj_set_event_cb(ui->BreathingLight_BL_sw_1, BreathingLight_BL_sw_1event_handler);
	lv_obj_set_event_cb(ui->BreathingLight_btn_1, BreathingLight_btn_1event_handler);
}

static void Voltmeter_btn_1event_handler(lv_obj_t * obj, lv_event_t event)
{
	switch (event)
	{
	case LV_EVENT_CLICKED:
	{
		if (!lv_debug_check_obj_valid(guider_ui.screen))
			setup_scr_screen(&guider_ui);
		lv_scr_load_anim(guider_ui.screen, LV_SCR_LOAD_ANIM_OVER_RIGHT, 200, 0, true);
	}
		break;
	default:
		break;
	}
}

void events_init_Voltmeter(lv_ui *ui)
{
	lv_obj_set_event_cb(ui->Voltmeter_btn_1, Voltmeter_btn_1event_handler);
}

static void Gyro_gyro2screenevent_handler(lv_obj_t * obj, lv_event_t event)
{
	switch (event)
	{
	case LV_EVENT_CLICKED:
	{
		if (!lv_debug_check_obj_valid(guider_ui.screen))
			setup_scr_screen(&guider_ui);
		lv_scr_load_anim(guider_ui.screen, LV_SCR_LOAD_ANIM_OVER_RIGHT, 300, 0, true);
		#if !LV_USE_GUIDER_SIMULATOR
		vTaskSuspend(mpuTaskHandle);    /* 挂起任务 */
		#endif
	}
		break;
	default:
		break;
	}
}

void events_init_Gyro(lv_ui *ui)
{
	lv_obj_set_event_cb(ui->Gyro_gyro2screen, Gyro_gyro2screenevent_handler);
}

static void thermometer_thermometer2screenevent_handler(lv_obj_t * obj, lv_event_t event)
{
	switch (event)
	{
	case LV_EVENT_CLICKED:
	{
		if (!lv_debug_check_obj_valid(guider_ui.screen))
			setup_scr_screen(&guider_ui);
		lv_scr_load_anim(guider_ui.screen, LV_SCR_LOAD_ANIM_OVER_RIGHT, 300, 0, true);
	}
		break;
	default:
		break;
	}
}

void events_init_thermometer(lv_ui *ui)
{
	lv_obj_set_event_cb(ui->thermometer_thermometer2screen, thermometer_thermometer2screenevent_handler);
}
